﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.OtherDecManager
{
    public partial class OtherDecList : Form
    {
        private OtherDecDAL dal = new OtherDecDAL();
        private int lineNum = 0;
        public OtherDecList()
        {
            InitializeComponent();
            this.dgmainDec.AutoGenerateColumns = false;
            this.dgdetaildec.AutoGenerateColumns = false;
        }
        //初始化
        private void OtherDecList_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        //关闭时操作
        private void OtherDecList_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.od = null;
        }
        //主附表联动
        private void dgmainDec_SelectionChanged(object sender, EventArgs e)
        {
            int count = this.dgmainDec.SelectedRows.Count;
            if (count <= 0)
            {
                this.dgdetaildec.DataSource = null;
                return;
            }
            else
            {
                this.dgdetaildec.DataSource = dal.GetOtherDetailList(this.dgmainDec.SelectedRows[0].Cells["oid"].Value.ToString());
            }
            
        }

        //新增类别
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            OtherMainEditing om = new OtherMainEditing();
            om.IsNew = true;
            
            if (om.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        //加载数据
        public void LoadData() {

            this.dgmainDec.DataSource = dal.GetOtherMainList(this.tbDecName.Text.Trim(),this.tbMxbhId.Text.Trim());
        }
        //主表编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = dgmainDec.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            lineNum = this.dgmainDec.SelectedRows[0].Index;
            OtherMainEditing oe = new OtherMainEditing();
            oe.IsNew = false;
            oe.EditingId = dgmainDec.SelectedRows[0].Cells["oid"].Value.ToString();
            if (oe.ShowDialog() == DialogResult.OK)
            {
                LoadData();
                dgmainDec.Rows[lineNum].Cells["decname"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgmainDec.CurrentCell = dgmainDec.Rows[lineNum].Cells["decname"];
            }
        }
        //主表删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = dgmainDec.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            string uid = dgmainDec.SelectedRows[0].Cells["oid"].Value.ToString();
            DataTable dt = dal.CheckOtherDetailDecByParentId(uid);
            if (dt.Rows.Count > 0) {
                MessageBox.Show("存在明细项，请先删除明细");
                return;
            }
            String info = "您确定要删除另加类：" + dgmainDec.SelectedRows[0].Cells["decname"].Value.ToString() + "吗";
            if (DialogResult.OK == MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                if (dal.DelOtherMainDecById(uid))
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                    LoadData();
                }
            }
        }
        //子表删除
        private void btnDel_Click(object sender, EventArgs e)
        {
            int count = dgdetaildec.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            lineNum = this.dgmainDec.SelectedRows[0].Index;
            string uid = dgdetaildec.SelectedRows[0].Cells["cid"].Value.ToString();
            String info = "您确定要删除另加类：" + dgdetaildec.SelectedRows[0].Cells["cdecname"].Value.ToString() + "吗";
            if (DialogResult.OK == MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                if (dal.DelOtherDetailDecById(uid))
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                    LoadData();
                }
                dgmainDec.Rows[lineNum].Cells["decname"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgmainDec.CurrentCell = dgmainDec.Rows[lineNum].Cells["decname"];
            }
        }
        //编辑子表
        private void btnEdit_Click(object sender, EventArgs e)
        {
            int count = dgdetaildec.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            lineNum = this.dgmainDec.SelectedRows[0].Index;
            OtherDetailEditing oe = new OtherDetailEditing();
            oe.IsNew = false;
            oe.EditingId = dgdetaildec.SelectedRows[0].Cells["cid"].Value.ToString();
            if (oe.ShowDialog() == DialogResult.OK)
            {
                LoadData();
                dgmainDec.Rows[lineNum].Cells["decname"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgmainDec.CurrentCell = dgmainDec.Rows[lineNum].Cells["decname"];
            }
        }
        //字表新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            
            //1、判断是否已经选中行
            int count = this.dgmainDec.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选择父类行");
                return;
            }
            lineNum = this.dgmainDec.SelectedRows[0].Index;
            OtherDetailEditing om = new OtherDetailEditing();
            om.ParentId = this.dgmainDec.SelectedRows[0].Cells["oid"].Value.ToString();
            om.mxbhid = this.dgmainDec.SelectedRows[0].Cells["mxbhid"].Value.ToString();
            om.IsNew = true;
            if (om.ShowDialog() == DialogResult.OK)
            {
                LoadData();
                dgmainDec.Rows[lineNum].Cells["decname"].Selected = true; //dgvFeeInfo为datagridview的名称
                dgmainDec.CurrentCell = dgmainDec.Rows[lineNum].Cells["decname"];
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
